package com.classtest.classtestdemo.controller;

import com.alibaba.fastjson.JSON;
import com.classtest.classtestdemo.bean.DbUser;
import com.classtest.classtestdemo.service.DbUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/server")
public class DbUserController {
    @Autowired
    DbUserService dbUserService;
    @RequestMapping(value = "/signup")
    public String userSignUpInsert(@RequestBody String jsonstr){
        Map<String,String> result = new HashMap<String,String>();
        String resFlag;
        try{
            if(jsonstr != null){
                if (dbUserService.exitUserNameInAll(jsonstr)){
                    dbUserService.userSignUpInsertAll(jsonstr);
                    resFlag = "ok";
                }else {
                    resFlag = "error";
                    result.put("resMessage","用户名已存在");
                }
            }else {
                resFlag = "error";
            }
        }catch (Exception e){
                return e.toString();
        }
        result.put("resFlag",resFlag);
        String res = JSON.toJSONString(result);
        return res;
    }


    @RequestMapping(value = "/usernameExitOrNot")
    public String usernameExitOrNot(@RequestBody String json){
        Map<String,String> result = new HashMap<String,String>();
        String resFlag = null;
        try{
            if (dbUserService.exitUserNameInAll(json)){
                resFlag = "ok";
            }else {
                resFlag = "error";
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        result.put("resFlag",resFlag);
        return JSON.toJSONString(result);
    }


    @RequestMapping(value = "/userLogin")
    public String userLogin(@RequestBody String json, HttpSession session){
        Map<String,String> result = new HashMap<String,String>();
        String resFlag = null;
        try{
            if (dbUserService.userLogin(json).isEmpty()){
                resFlag = "error";
                result.put("loginMsg","登录成功");
                result.put("resDesc","账号密码错误");
            }else {
                List<DbUser> dbUsers = dbUserService.userLogin(json);
                DbUser dbUser = dbUsers.get(0);
                if (StringUtils.isNotBlank(dbUser.getRedistrationType())){
                    if (dbUser.getRedistrationType().equals("普通用户")){
                        result.put("loginMsg","1");
                        result.put("resDesc","");
                        resFlag = "ok";
                        session.setAttribute("user",dbUser);
                    }else if (dbUser.getRedistrationType().equals("普通卖家")){
                        result.put("loginMsg","0");
                        result.put("resDesc","请到卖家主页进行登录");
                        resFlag = "ok";
                    }
                }
            }
        }catch (Exception e){
            resFlag = "error";
            result.put("resFlag",resFlag);
            result.put("error",e.toString());
            result.put("resDesc","账号密码错误");
        }
        result.put("resFlag",resFlag);
        return JSON.toJSONString(result);
    }
}
